<?php
namespace TestHome\Controller;
use Think\Controller;
class StartQuizController extends BaseController {
    /**
     * 显示问卷简介
     */

    public function index($quiz_id)
    {
        $M = M("quiz_info");
        $data = $M->where(['quiz_id' => $quiz_id])->select();
        $page_quiztitle = $data[0]['quiz_pagetitle'];
        $page_quizintroduce = $data[0]['quiz_pageintroduce'];
        $page_quiztips = $data[0]['quiz_pagetips'];

        $this->assign('quiz_title', $page_quiztitle);
        $this->assign('quiz_introduce', $page_quizintroduce);
        $this->assign('quiz_tips', $page_quiztips);
        $this->assign('quiz_id', $quiz_id);

       // var_dump($data);
        $this->display();

    }

    public function doquiz($quiz_id)
    {
        $q = M("question")->where(["quiz_id" => $quiz_id]);
        $qcount = $q->count();
        $this->assign('count', $qcount);
        $this->assign('quiz_id', $quiz_id);
        $this->assign('userinfo', parent::$userinfo);
        $quiz_title = M('quiz_info')->where(['quiz_id' => $quiz_id])->select();
        $this->assign('quiz_title', $quiz_title[0]['quiz_pagetitle']);
        $this->display();
    }


    public function getQuestion($quiz_id)
    {

        $questions = M('question');
        $answers = M('user_answer');
        $a = $answers->where(['user_id' => parent::$uid, 'quiz_id' => $quiz_id])->max('qst_id');//找最大题号
        $result = [];
        $count = $questions->where(['quiz_id' => $quiz_id])->count();

        if (!$a){
            //做过题的记录是空，获取第一道题

            $result = $questions->where(['quiz_id' => $quiz_id, 'qst_id' => $questions->min('qst_id')])->select();//找第一道题
            $result = $result[0];
            $result['count'] =  $count;

        }

        else {
            //检查上一道题目答案是否已经入库（待考察）

            if ($a + 1 > $count) {
                $rs['code'] = -1;
                $rs['msg'] = '题目你都做完了，可以直接查看报告';
                $this->ajaxReturn($rs);
            } else {
                $result = $questions->where(['quiz_id' => $quiz_id, 'qst_id' => $a + 1])->select();
                $result = $result[0];
                $result['count'] =  $questions->where(['quiz_id' => $quiz_id])->count();

            }
        }

        //筛选出问题列表
        for($i = 1; $i <=6; $i ++){
            $optkey = "option".$i;
            if($result[$optkey] != null){
                $questionData[$optkey] = $result[$optkey];
            }
            else{
                break;
            }
        }


        $questionData['count'] = $result['count'];
        $questionData['qst_commant'] = $result['qst_commant'];
        $questionData['qst_id'] = $result['qst_id'];


        $this->ajaxReturn($questionData);

    }


    public function submitAnswer($quiz_id)
    {
        if (IS_AJAX){
            $qid = I("post.qid");
            $answer = I("post.answer");
            $questions = M('user_answer');

            $data['user_id'] = parent::$uid;
            $data['quiz_id'] = $quiz_id;
            $data['qst_id'] = $qid;
            $data['selection'] = $answer;
            $questions->add($data);

            $rs['code'] = 1;
            $this->ajaxReturn($rs);
        }

    }


    public function calcAnswer($quiz_id)
    {

        if ($quiz_id == 6){
            $calc = A("Quiz/Pf16");
            if ($calc->calcAnswer(parent::$uid)){
                $rs['code'] = 1;
                $rs['msg'] = "计算完成";
            }
            else{
                $rs['code'] = 0;
                $rs['msg'] = "计算失败，查阅系统日志";
            }

            $this->ajaxReturn($rs);
        }


        else if ($quiz_id == 7){
            $calc = A("Quiz/Wippf");
            if ($calc->calcAnswer(parent::$uid)){
                $rs['code'] = 1;
                $rs['msg'] = "计算完成";
            }
            else{
                $rs['code'] = 0;
                $rs['msg'] = "计算失败，查阅系统日志";
            }

            $this->ajaxReturn($rs);
        }

        else if ($quiz_id == 12){
            $calc = A("Quiz/Mbti");
            if ($calc->calcAnswer(parent::$uid)){
                $rs['code'] = 1;
                $rs['msg'] = "计算完成";
            }
            else{
                $rs['code'] = 0;
                $rs['msg'] = "计算失败，查阅系统日志";
            }
            $this->ajaxReturn($rs);
        }
        //计算自定义问卷
        else{
            $calc = A("Quiz/CustomQuiz");
            if ($calc->calcAnswer($quiz_id ,parent::$uid)){
                $rs['code'] = 1;
                $rs['msg'] = "计算完成";
            }
            else{
                $rs['code'] = 0;
                $rs['msg'] = "计算自定义问卷失败，查阅系统日志";
            }

            $this->ajaxReturn($rs);
        }

    }



    //测试填充数据

    public function randomAnswer($uid)
    {

        $dao = M('user_answer');
        if ($uid){
            $user_id = $uid;
        }
        else{
            $user_id = parent::$uid;
        }

        for ($qid = 1; $qid <= 184; $qid++){
            $data['user_id'] = $user_id;
            $data['selection'] = rand(1,3);
            $data['qst_id'] = $qid;
            $data['quiz_id'] = 1;
            $dao->add($data);
        }


        $calctl = A("Type16pf");
        $calctl->calcAnswer($user_id);
        echo "finish";

    }



}